package main.flex.manager.section
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import main.flex.conf.MediaConf;
	import main.flex.value.MediaListTO;
	import main.flex.widget.section.WiiavieMediaWidget;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.servebox.foundry.widget.WidgetManager;

	public class WiiavieMediaWidgetManager extends WidgetManager
	{
		[Bindable]
		private var _espaceId		:		int;
		private var _mediaList		:		MediaListTO;
		private var fileRef			:		FileReference;
		
		public function WiiavieMediaWidgetManager()
		{
			super();
		}
		
		[Bindable("mediaListChanged")]
		public function get mediaList() : MediaListTO
		{
			return _mediaList;
		}
		
		public function set mediaList ( val : MediaListTO ) : void
		{
			_mediaList = val;
			dispatchEvent( new Event ( "mediaListChanged" ) );
		}
		
		public function set espaceId(val : int) : void
		{
			_espaceId = val;
		}
		
		public function browseAndUpload():void
		{
			fileRef = new FileReference();
			fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileRef_error);
			fileRef.addEventListener(Event.SELECT, fileRef_select);
			fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
			fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
			fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			fileRef.browse();
		}
		
		private function fileRef_select(evt:Event):void {
            try {
              	currentWidget.chemin.text=fileRef.name;
              	currentWidget.uploader.enabled=true;
            } catch (err:Error) {
                currentWidget.chemin.text=err.message;
            }
        }
        
        private function fileRef_error(pEvt:IOErrorEvent):void
        {
        	Alert.show(pEvt.text);
        }
        
        private function fileRef_progress(evt:ProgressEvent):void {
            currentWidget.progressBar.width = 100;
        }
        
         private function fileRef_complete(evt:Event):void {
            currentWidget.progressBar.visible = false;
        }
           
		private function httpStatusHandler(event:HTTPStatusEvent):void {
            
         if (event.status != 200)
         {
            if (event.status == 500){
                mx.controls.Alert.show("Probleme d'écriture","Error");
                return;
            }
              if (event.status == 404){
                mx.controls.Alert.show("Fichier non trouvé","Error");
                return;
            }
             if (event.status == 415){
                mx.controls.Alert.show("Type de media interdit","Error");
                return;
            }
             if (event.status == 401){
                mx.controls.Alert.show("Acces refusé","Error");
                return;
            }
            mx.controls.Alert.show("Erreur "+event.status,"Error");
         }
        }

		private function refresh(pEvt:FlexEvent):void
		{
			var timer:Timer=new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,test);
		}
		
		private function test(pEvt:TimerEvent):void
		{
			currentWidget.invalidateDisplayList();
		}
		
		public function upload():void
		{
			fileRef.upload(new URLRequest(MediaConf.FILE_UPLOAD_URL+_espaceId.toString()));
			fileRef.addEventListener(Event.COMPLETE, uploadFinished);
		}
		
		public function uploadFinished( pEvent : Event) :void
		{
			currentWidget.refreshMedia();
			currentWidget.currentState='';
		}
		
		public function mediaSuivant():void
		{
			if(currentWidget.coverflow.selectedIndex+1<currentWidget.coverflow.numChildren)
			{
				currentWidget.coverflow.selectedIndex=currentWidget.coverflow.selectedIndex+1;
			}
		}
				
		/* Shortcuts */
		public function get currentWidget() : WiiavieMediaWidget
		{
			return WiiavieMediaWidget( getWidget() );
		}
	}
}